package cn.player.media.entity;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import cntv.player.core.util.Utils;
import cn.player.media.util.ParseUtil;

public class AdEvent implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String time;
	private String url;
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	
	/**
	 * 存取time = 1时的url值，重复覆盖保存
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Map<Integer,AdEvent> getEvents(JSONObject obj) throws Exception{
		Map<Integer,AdEvent> adEvents = new HashMap<Integer,AdEvent>();
		JSONArray events = obj.optJSONArray("events"); 
		for(int i=0;i<events.length();i++){
			JSONObject event = events.getJSONObject(i);
			int time = Utils.strToInt(event.optString("time"));
			if(time == 1 || time == -1){
				adEvents.put(time, ParseUtil.parseDataToEntity(event, AdEvent.class));
			}
		}
		return adEvents;
		
	}
}
